package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.sys.SysNoticeDao;
import com.framework.entity.push.PushConfig;
import com.framework.entity.push.PushContent;
import com.framework.entity.sys.SysDeptJumpUrl;
import com.framework.entity.sys.SysNotice;
import com.framework.page.PageUtils;
import com.framework.service.PushConfigService;
import com.framework.service.SysDeptJumpUrlService;
import com.framework.service.SysNoticeService;
import com.framework.utils.push.JiGuangPush;
import com.framework.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 通知列表
 * @author wq
 */
@Service("sysNoticeServiceImpl")
@Transactional(rollbackFor = Exception.class)
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeDao,SysNotice> implements SysNoticeService {
	@Autowired
	private PushConfigService pushConfigService;

	@Autowired
	private SysDeptJumpUrlService sysDeptJumpUrlService;

	@Override
	public PageUtils queryNoticeList(Map<String, Object> params) {
		Integer limit= Integer.valueOf(params.get("limit").toString());
		Integer page= Integer.valueOf(params.get("page").toString());
		params.put("start",(page-1)*limit);
		params.put("limit",limit);
		Long deptId= Long.valueOf(params.get("deptId").toString());
		List<SysNotice> list = this.baseMapper.queryNoticeList(params);
		Integer count = this.baseMapper.queryNoticeCount(deptId);
		return new PageUtils(list,count,limit,page);
	}

	@Override
	public void insertNewNotice(String coverUrl,Long deptId, HttpServletRequest request) {
		SysNotice sysNotice = new SysNotice();
		String noticeNO= StringUtil.getDateString(7);
		noticeNO=verifyNoticeNo(noticeNO);
		String content=request.getParameter("content");
		String titles=request.getParameter("titles");
		String remark=request.getParameter("remark");
		sysNotice.setSysNoticeNo(noticeNO);
		sysNotice.setTitles(titles);
		sysNotice.setContent(content);
		sysNotice.setCreateDate(new Date());
		sysNotice.setRemark(remark);

		SysDeptJumpUrl sysDeptJumpUrl=sysDeptJumpUrlService.queryByDeptIdAndType(deptId,1);
		sysNotice.setJumpUrl(sysDeptJumpUrl.getNoticeJump());

		sysNotice.setIsShow(1);
		sysNotice.setDeptId(deptId);
		sysNotice.setCoverImg(coverUrl);
		this.baseMapper.addNotice(sysNotice);

		SysNotice notice = baseMapper.selectById(noticeNO);
		PushConfig pushConfig = pushConfigService.queryPushConfigByDeptId(deptId);
		if(pushConfig!=null){
			//配置推送内容
			PushContent pushContent=new PushContent();
			pushContent.setPushType("all");
			pushContent.setNoticeType("sys");
			pushContent.setTitle("系统通知");
			pushContent.setAlert(notice.getTitles());
			pushContent.setJumpUrl(notice.getJumpUrl());
			pushContent.setSysNoticeNo(notice.getSysNoticeNo());
			//推送
			JiGuangPush.jiguangPush(pushContent,pushConfig);

		}

	}
	private String verifyNoticeNo(String noticeNO) {

		SysNotice n = baseMapper.selectById(noticeNO);
		if(n!=null){
			noticeNO=StringUtil.getDateString(7);
			verifyNoticeNo(noticeNO);
		}
		return noticeNO;
	}


}

